package com.hantanger.weblog.dao;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.objectify.cmd.Query;
import com.hantanger.weblog.model.Comment;
import com.hantanger.weblog.util.DaoHelper;


public class CommentDao {

	public void addComment(Comment comment) {
		DaoHelper.save(comment);
	}

	public void delete(long id) {
		DaoHelper.delete(Comment.class, id);
	}

	public List<Comment> getParentCommentList(long postId, int limit, long publishTime) {
		if (postId <= 0) {
			return null;
		}
		Query<Comment> query = DaoHelper.getLoader().type(Comment.class)
				.filter("postId", postId).filter("parentId", 0L).filter("updateTime <", publishTime)
				.limit(limit).order("-updateTime");
		List<Comment> comments = new ArrayList<Comment>();
		for (Comment comment : query) {
			comments.add(comment);
		}
		return comments;
	}
	
	public List<Comment> getSubCommentList(long parentCommentId, int limit, long publishTime) {
		if (parentCommentId <= 0) {
			return null;
		}
		Query<Comment> query = DaoHelper.getLoader().type(Comment.class)
				.filter("parentId", parentCommentId).filter("updateTime <", publishTime)
				.limit(limit).order("-updateTime");
		List<Comment> comments = new ArrayList<Comment>();
		for (Comment comment : query) {
			comments.add(comment);
		}
		return comments;
	}
	
	public Comment getCommentById(long id) {
		return DaoHelper.get(Comment.class, id);
	}

}
